package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.domain.AppInfo;
import com.itcast.googleplay.utils.UIUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class AppDesHolder extends BaseHolder<AppInfo> implements OnGlobalLayoutListener {

	private static final String TAG = "AppDesHolder";
	private TextView tvDes;
	private TextView tvAuthor;
	private ImageView ivArrow;
	private int minHeight;
	private int maxHeight;
	private boolean isOpen = false;
	private ValueAnimator valueAnimator;
	private ScrollView scrollView;
	//查找到的scrollView
	public void setScrollView(ScrollView scrollView) {
		this.scrollView = scrollView;
	}
	
	@Override
	public void initData() {
		AppInfo data = getData();
		tvDes.setText(Html.fromHtml(data.des));
		tvAuthor.setText(data.author);
		//现在textView在显示文本的时候的高度,为5行,5行就是一个起始高度大小,像素值
		tvDes.setMaxLines(5);
		
		//智慧北京---->先要将页面构建出来后,再去获取控件的高度,监听树状结构的生成(页面的构建)
		tvDes.getViewTreeObserver().addOnGlobalLayoutListener(this);
//		//onMeasure ----->onLayout------>onDraw
		
		
		//给tvDes做点击事件的监听,让其能够扩展和收缩
		tvDes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				expend();
			}
		});
	}

	protected void expend() {
		if(!isOpen){
			//1,点击前是关闭状态,点击后就需要扩展
			isOpen = true;
			
			//如果服务器的数据,确实不止5行能够显示完全,那就需要做扩展动画
			//如果服务器5行就可以显示完全,没必要再去执行动画
			if(minHeight<maxHeight){
				valueAnimator = ValueAnimator.ofInt(minHeight,maxHeight);
			}
		}else{
			isOpen = false;
			if(minHeight<maxHeight){
				valueAnimator = ValueAnimator.ofInt(maxHeight,minHeight);
			}
		}
		
		//2.做valueAnimator非空判断
		if(valueAnimator!=null){
			//3.扩展的事件监听
			valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator valueAnimator) {
					int height = (Integer) valueAnimator.getAnimatedValue();
					LayoutParams layoutParams = tvDes.getLayoutParams();
					layoutParams.height = height;
					tvDes.setLayoutParams(layoutParams);
					
					//文本高度的变化,可能会导致文本显示不全,当文本显示不全的时候,scrollView全屏滚动
					if(scrollView!=null){
						scrollView.fullScroll(ScrollView.FOCUS_DOWN);
					}
				}
			});
			
			valueAnimator.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator arg0) {
					
				}
				
				@Override
				public void onAnimationRepeat(Animator arg0) {
					
				}
				
				@Override
				public void onAnimationEnd(Animator arg0) {
					if(isOpen){
						//扩展开的,箭头朝上
						ivArrow.setBackgroundResource(R.drawable.arrow_up);
					}else{
						//收缩,箭头朝下
						ivArrow.setBackgroundResource(R.drawable.arrow_down);
					}
				}
				
				@Override
				public void onAnimationCancel(Animator arg0) {
					
				}
			});
			valueAnimator.setDuration(300);
			valueAnimator.start();
		}
	}

	@Override
	public View initView() {
		View view = UIUtils.inflate(R.layout.layout_app_detail_des);
		tvDes = (TextView) view.findViewById(R.id.tv_des);
		tvAuthor = (TextView)view.findViewById(R.id.tv_author);
		ivArrow = (ImageView) view.findViewById(R.id.iv_arrow);
		return view;
	}

	@Override
	public void onGlobalLayout() {
		//2.在系统已经帮助测量后,获取tvDes高度大小
		minHeight = tvDes.getMeasuredHeight();
		Log.i(TAG, "minHeight = "+minHeight);//97
		//3.获取完整显示文本的高度,在此处定义成1000行的目的是为了让textView能够完全显示,然后再去监听其完全显示的高度大小
		tvDes.setMaxLines(1000);//给你提供1000行供你使用<=1000
		//移除上一次对layout的监听
		tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
		
		//4.再次监听当前tvDes控件在布局文件中显示出来
		tvDes.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				//5.监听获取完全显示文本的高度
				maxHeight = tvDes.getMeasuredHeight();
				Log.i(TAG, "maxHeight = "+maxHeight);//269
				//移除本次对layout完成的监听
				tvDes.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				
				//6.将textView设置成5行显示
				LayoutParams layoutParams = tvDes.getLayoutParams();
				layoutParams.height = minHeight;
				tvDes.setLayoutParams(layoutParams);
			}
		});
	}
}
